package com.sky.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * @author shd
 * @description
 * @date 2025-01-20 19:43
 */
@Aspect
@Slf4j
@Component
public class AutoFillAspect {

    @Around("@annotation(com.sky.annotation.AutoFill)")
    public Object fillProperty(ProceedingJoinPoint pjp) {
        try {
            log.info("{}", pjp);
            Object[] args = pjp.getArgs();
            if (ObjectUtil.isEmpty(args)) {
                //如果参数是空说明，肯定是不增改
                return pjp.proceed();
            }
            Object pojo = args[0];
            //一开始我想的是把object换成employee，但不能，因为Object还可能是category所以动态来确定，这也是为什么要用反射的原因
            LocalDateTime now = LocalDateTime.now();
            Long empId = BaseContext.getCurrentId();

            //Signature signature = pjp.getSignature();
            //log.warn("{}, {}, {}, {}", signature.getDeclaringType(), signature.getDeclaringTypeName(), signature.getName(), signature.getModifiers());
            //只有methodSignature有getMethod的方法，signature没有这个方法,因为signature只是一个接口
            MethodSignature signature = (MethodSignature) pjp.getSignature();
            Method method = signature.getMethod();
            AutoFill autoFill = method.getAnnotation(AutoFill.class);
            if (ObjectUtil.isEmpty(autoFill)) {
                //如果注解是空说明
                return pjp.proceed();
            }
            OperationType value = autoFill.value();
            Class<?> clazz = pojo.getClass();
            if (OperationType.INSERT == value) {
                Method setCreateTime = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = clazz.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method setUpdateTime = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setCreateTime.invoke(pojo, now);
                setCreateUser.invoke(pojo, empId);
                setUpdateTime.invoke(pojo, now);
                setUpdateUser.invoke(pojo, empId);

            } else if (OperationType.UPDATE == value) {
                Method setUpdateTime = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = clazz.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);
                setUpdateTime.invoke(pojo, now);
                setUpdateUser.invoke(pojo, empId);
            }
            return pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
            return null;
        }
    }
}
